var roleRepairer = {

    /** @param {Creep} creep **/
    run: function(creep) {
        //creep.say(creep.memory.building);
        if(creep.store[RESOURCE_ENERGY] == 0){
            creep.memory.building = true;
        }
        else{
            creep.memory.building = false;
        }

        if(creep.memory.building == true){
            //creep.say(creep.memory.building);
            var targetsgo = creep.room.find(FIND_STRUCTURES, {
                filter: (structure) => {
                    return structure.structureType == STRUCTURE_STORAGE && structure.store.getUsedCapacity() > 0;
                }
            });
            if(targetsgo.length > 0) {
                if(creep.withdraw(targetsgo[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targetsgo[0]);
                    //creep.say('修来');
                }
            }   

        }
        else{
            const targets = creep.room.find(FIND_STRUCTURES, {
                filter: (structure) => structure.hits < structure.hitsMax && 
                (structure.structureType == STRUCTURE_WALL || structure.structureType == STRUCTURE_RAMPART)
            });
            targets.sort((a,b) => a.hits - b.hits);
            if(targets.length > 0) {
                if(creep.repair(targets[0]) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targets[0],{visualizePathStyle: {stroke: '#ffaa00'}});
                    //creep.say('修去');
                }
            }
            
        }
	}
};

module.exports = roleRepairer;